package com.ytt.core.service.service.college;


import com.ytt.core.model.college.CollegeDto;
import com.ytt.core.model.college.CollegeEditDto;
import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.service.entity.college.College;
import com.ytt.core.service.exception.NoSuchDataException;
import com.ytt.core.service.mapper.college.CollegeMapper;
import com.ytt.core.service.repository.CollegeRepository;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.utils.BaseUtil;
import com.ytt.core.service.utils.PageDataUtil;
import com.ytt.core.util.jpa.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service("collegeService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CollegeImpl  implements  CollegeService{
    private  final CollegeRepository collegeRepository;
    private final CollegeMapper collegeMapper;

    @Autowired
    public CollegeImpl(CollegeRepository collegeRepository, CollegeMapper collegeMapper) {
        this.collegeRepository = collegeRepository;
        this.collegeMapper = collegeMapper;
    }


    @Override
    public CollegeDto findById(String id) {
        College notification = this.collegeRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        return this.collegeMapper.toDTO(notification);
    }

    @Override
    public List<CollegeDto> findAll(String sort, ProposalQueryParam param) {
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<College> notifications = this.collegeRepository.findAll(
                BaseUtil.<College>getBuilder(param).build(), sort1);
        return notifications.stream().map(this.collegeMapper::toDTO).collect(Collectors.toList());
    }

    @Override
    public PageDataDTO<CollegeDto> findOnePage(Integer page, Integer size, String sort, ProposalQueryParam param) {
        Page<College> notificationPage = this.collegeRepository.findAll(
                BaseUtil.<College>getBuilder(param).build(), PageHelper.generatePageRequest(page, size, sort));
        List<CollegeDto> notificationDTOList = notificationPage.getContent().stream().map(this.collegeMapper::toDTO)
                .collect(Collectors.toList());
        return PageDataUtil.toPageData(notificationPage, notificationDTOList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CollegeDto create(CollegeEditDto saveDTO) {
        College entity = new College();
        this.collegeMapper.updateEntity(saveDTO, entity);
        entity = this.collegeRepository.save(entity);
        return this.collegeMapper.toDTO(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CollegeDto update(String id, CollegeEditDto saveDTO) {
        Optional<College> optional = this.collegeRepository.findById(Long.valueOf(id));
        College entity = optional.orElseThrow(() -> new NoSuchDataException(id));

        this.collegeMapper.updateEntity(saveDTO, entity);
        entity = this.collegeRepository.saveAndFlush(entity);
        return this.collegeMapper.toDTO(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        College entity = this.collegeRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        this.collegeRepository.delete(entity);
    }
}
